import {Injectable} from '@angular/core';
import {BehaviorSubject, Observable} from 'rxjs';
import {MockConfigItem} from '../entity/mock-config-item';
import {MockConfig} from '../entity/mock-config';
import {TpHttpService} from './tp-http.service';
import {BaseResponse} from '../entity/base-response';

@Injectable({
    providedIn: 'root'
})
export class MockService {
    private mockConfigItemBehavior = new BehaviorSubject<MockConfigItem>(null);

    mockConfigItemObservable = this.mockConfigItemBehavior.asObservable();

    constructor(private httpService: TpHttpService) {
    }

    public activeMockConfigItem(item: MockConfigItem) {
        this.mockConfigItemBehavior.next(item);
    }

    save(mockConfig: MockConfig): Observable<BaseResponse<string>> {
        return this.httpService.post('/api/mock', mockConfig);
    }

    list(docId: string, apiId: string): Observable<BaseResponse<MockConfig[]>> {
        return this.httpService.get('/api/mock/' + docId + '/' + apiId + '/');
    }

    getById(docId: string, apiId: string, mockId: string): Observable<BaseResponse<MockConfig>> {
        return this.httpService.get('/api/mock/' + docId + '/' + apiId + '/' + mockId);
    }

    preview(mockConfig: MockConfig): Observable<string> {
        return this.httpService.post('/api/mock/test', mockConfig);
    }

    delete(docId: string, apiId: string, mockId: string): Observable<BaseResponse<any>> {
        return this.httpService.del('/api/mock/' + docId + '/' + apiId + '/' + mockId);
    }
}
